package com.ys.erp.stage_config_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ys.erp.entity.data.bean.Employee;
import com.ys.erp.entity.data.bean.ProcureTransportSetting;
import com.ys.erp.entity.data.bean.ShopTypeProcureDays;
import com.ys.erp.entity.service.IEmployeeService;
import com.ys.erp.entity.service.IProcureTransportSettingService;
import com.ys.erp.entity.service.IShopTypeProcureDaysService;
import com.ys.erp.framework.bean.dto.StageEmpDTO;
import com.ys.erp.framework.exception.YsException;
import com.ys.erp.stage_config_service.bean.condition.ProcureSettingCondition;
import com.ys.erp.stage_config_service.bean.vo.ProcureTransportVO;
import com.ys.erp.stage_config_service.bean.vo.TypeProcureDayVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 南山
 * @date 2025/07/24 14:40
 */
@Service
@RequiredArgsConstructor
public class ProcureSettingService {
    private final IShopTypeProcureDaysService iShopTypeProcureDaysService;
    private final IProcureTransportSettingService iProcureTransportSettingService;
    private final IEmployeeService iEmployeeService;

    public List<TypeProcureDayVO> getProcureDayList(StageEmpDTO empDto) {
        List<ShopTypeProcureDays> shopTypeProcureDaysList = iShopTypeProcureDaysService.list();
        return shopTypeProcureDaysList.stream().map(shopTypeProcureDays -> {
            TypeProcureDayVO shopTypeProcureDayVO = new TypeProcureDayVO();
            shopTypeProcureDayVO.setId(shopTypeProcureDays.getId());
            shopTypeProcureDayVO.setShopType(shopTypeProcureDays.getShopType());
            shopTypeProcureDayVO.setProcureDay(shopTypeProcureDays.getDayNum());
            return shopTypeProcureDayVO;
        }).toList();
    }


    public void addOrUpdateProcureDay(ProcureSettingCondition.AddOrUpdateProcureDayCon con, StageEmpDTO empDto) {
        QueryWrapper<ShopTypeProcureDays> shopTypeProcureDaysQw = new QueryWrapper<>();
        shopTypeProcureDaysQw.lambda().eq(ShopTypeProcureDays::getShopType, con.getShopType());
        shopTypeProcureDaysQw.lambda().ne(ShopTypeProcureDays::getId, con.getId());
        long count = iShopTypeProcureDaysService.count(shopTypeProcureDaysQw);
        if (count > 1) {
            throw new YsException(-1, "该店铺类型已存在采购设置,无法添加或修改");
        }
        if (Objects.isNull(con.getId())) {
            ShopTypeProcureDays shopTypeProcureDays = new ShopTypeProcureDays();
            shopTypeProcureDays.setDayNum(con.getProcureDay());
            shopTypeProcureDays.setShopType(con.getShopType());
            shopTypeProcureDays.setEmpId(empDto.getId());
            iShopTypeProcureDaysService.save(shopTypeProcureDays);
            return;
        }
        ShopTypeProcureDays shopTypeProcureDays = iShopTypeProcureDaysService.getById(con.getId());
        if (Objects.isNull(shopTypeProcureDays)) {
            throw new RuntimeException("源数据不存在，无法修改");
        }
        shopTypeProcureDays.setDayNum(con.getProcureDay());
        shopTypeProcureDays.setShopType(con.getShopType());
        shopTypeProcureDays.setEmpId(empDto.getId());
        iShopTypeProcureDaysService.updateById(shopTypeProcureDays);
    }

    public void delProcureDay(Long id) {
        ShopTypeProcureDays shopTypeProcureDays = iShopTypeProcureDaysService.getById(id);
        if (Objects.isNull(shopTypeProcureDays)) {
            throw new YsException(-1, "源数据不存在，无法删除");
        }
        iShopTypeProcureDaysService.removeById(id);
    }

    public void addOrUpdateProcureTransport(ProcureSettingCondition.AddOrUpdateProcureTransportCon con, StageEmpDTO empDto) {
        if (Objects.isNull(con.getId())) {
            ProcureTransportSetting procureTransportSetting = new ProcureTransportSetting();
            procureTransportSetting.setEmpId(empDto.getId());
            BeanUtils.copyProperties(con, procureTransportSetting);
            iProcureTransportSettingService.save(procureTransportSetting);
            return;
        }
        ProcureTransportSetting procureTransportSetting = iProcureTransportSettingService.getById(con.getId());
        if (Objects.isNull(procureTransportSetting)) {
            throw new YsException(-1, "源数据不存在，无法修改");
        }
        BeanUtils.copyProperties(con, procureTransportSetting);
        procureTransportSetting.setEmpId(empDto.getId());
        iProcureTransportSettingService.updateById(procureTransportSetting);

    }

    public void delProcureTransport(StageEmpDTO empDto) {
        ProcureTransportSetting byId = iProcureTransportSettingService.getById(empDto.getId());
        if (Objects.isNull(byId)) {
            throw new YsException(-1, "源数据不存在，无法删除");
        }
        iProcureTransportSettingService.removeById(empDto.getId());
    }

    public List<ProcureTransportVO> getProcureTransportList(StageEmpDTO empD) {
        QueryWrapper<ProcureTransportSetting> procureTransportSettingQueryWrapper = new QueryWrapper<>();
        procureTransportSettingQueryWrapper.lambda().orderByDesc(ProcureTransportSetting::getCreateTime);
        procureTransportSettingQueryWrapper.lambda().orderByDesc(ProcureTransportSetting::getIsSticky);
        procureTransportSettingQueryWrapper.lambda().orderByDesc(ProcureTransportSetting::getUpdateTime);
        List<ProcureTransportSetting> procureTransportSettings = iProcureTransportSettingService.list();
        if (CollectionUtils.isEmpty(procureTransportSettings)) {
            return new ArrayList<>();
        }
        List<Long> empList = procureTransportSettings.stream().map(ProcureTransportSetting::getEmpId).toList();
        Map<Long, Employee> empMap = iEmployeeService.getEmpMapByEmpIdList(empList);
        ArrayList<ProcureTransportVO> results = new ArrayList<>();
        for (ProcureTransportSetting procureTransportSetting : procureTransportSettings) {
            ProcureTransportVO procureTransportVO = new ProcureTransportVO();
            Employee employee = empMap.get(procureTransportSetting.getEmpId());
            procureTransportVO.setCreateBy(employee.getRealName());
            BeanUtils.copyProperties(procureTransportSetting, procureTransportVO);
            results.add(procureTransportVO);
        }
        return results;
    }

    public void isSticky(Long id, StageEmpDTO empDto) {
        ProcureTransportSetting byId = iProcureTransportSettingService.getById(id);
        if (Objects.isNull(byId)) {
            return;
        }
        byId.setIsSticky(byId.getIsSticky() == 1 ? 0 : 1);
        byId.setEmpId(empDto.getId());
        iProcureTransportSettingService.updateById(byId);
    }
}
